package com.nian.db.hive.service.impl;

import com.nian.db.hive.model.MemberModel;
import com.nian.db.hive.service.IHiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/25 0025 18:34
 */
@Slf4j
@Service
public class HiveServiceImpl<T> implements IHiveService<T> {

    @Autowired
    JdbcTemplate hiveJdbcTemplate;


    @Override
    public void createTable(String createTableSql) {
        hiveJdbcTemplate.execute(createTableSql);

    }

    @Override
    public List listAll(String tableName) {
        String sql = "select * from " + tableName;
        return hiveJdbcTemplate.queryForList(sql);
    }

    /**
     *
     * @param pathFile 目录文件为服务器地址
     */
    @Override
    public void loadLocalData(String pathFile, String tableName) {
        String sql = "LOAD DATA LOCAL INPATH  '"+pathFile+"' INTO TABLE " + tableName;
        log.info("loadLocalData SQL: {}", sql);
        hiveJdbcTemplate.execute(sql);
    }

    @Override
    public void deleteAll() {
        String sql = "insert overwrite table member select * from member where 1=0";
        hiveJdbcTemplate.execute(sql);
    }

    @Override
    public void execute(String sql) {
        log.info("execute SQL: {}", sql);
        hiveJdbcTemplate.execute(sql);
    }

    @Override
    public List executeQuery(String sql) {
        return hiveJdbcTemplate.queryForList(sql);
    }
}
